﻿using SCG.General;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TTEngine
{
    public class Fire : MapObject
    {
        //constructor
        public Fire()
            : base()
        {
            Damage = new DamageExpressionList("1d6=f");
        }

        //clone
        public override MapObject Clone()
        {
            return new Fire();
        }

        //immuminate
        public override ByteMatrix IllumMap
        {
            get
            {
                if (_illumMap == null)
                    _illumMap = new ByteMatrix(Properties.Resources.FireMap);
                return _illumMap;
            }
        }

        //burn anything there, and next to it
        public override void ProcessImpulse()
        {
            base.ProcessImpulse();
            BurnItemsAt(X, Y);
            for (int x = -1; x <= 1; x++)
                for (int y = -1; y <= 1; y++)
                    if (x != 0 || y != 0)
                        BurnItemsAt(X + x, Y + y);

            //burn actor in the square
            Actor burned = Level.GetActorAt(X, Y, false);
            if (burned != null)
            {
                string msg = burned.DisplayName + " is burned!";
                Level.Host.BloodSplatter(X, Y);
                foreach (DamageExpression de in Damage)
                {
                    burned.ApplyDamage(de.DamageValue, de.DamageType, msg);
                    msg = "";
                }
            }
        }
        public void BurnItemsAt(int x, int y)
        {
            List<MapObject> lst = Level.ObjectMatrix[x, y];
            if (lst != null)
            {
                List<MapObject> burn = new List<MapObject>(lst);
                foreach (MapObject burnObject in burn)
                    if (Globals.RandomIntFromTo(1, 100) < burnObject.Flamability)
                        burnObject.Burn();
            }
        }

        //private members
        private static ByteMatrix _illumMap = null;
    }
}
